package org.linlinjava.litemall.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.linlinjava.litemall.db.dao.OrderGoodsMapper;
import org.linlinjava.litemall.db.models.OrderGoods;
import org.linlinjava.litemall.db.service.OrderGoodsService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderGoodsServiceImpl extends ServiceImpl<OrderGoodsMapper, OrderGoods>
    implements OrderGoodsService {

    @Override
    public List<OrderGoods> queryByOid(Long orderId) {
        QueryWrapper<OrderGoods> query = new QueryWrapper<>();
        query.eq("order_id", orderId);
        return baseMapper.selectList(query);
    }

    @Override
    public List<OrderGoods> findByOidAndGid(Long orderId, Long goodsId) {
        QueryWrapper<OrderGoods> query = new QueryWrapper<>();
        query.eq("order_id", orderId);
        query.eq("goods_id", goodsId);
        return baseMapper.selectList(query);
    }

    @Override
    public Short getComments(Long orderId) {
        QueryWrapper<OrderGoods> query = new QueryWrapper<>();
        query.eq("order_id", orderId);
        int count = baseMapper.selectCount(query);
        return (short) count;
    }

    @Override
    public boolean checkExist(Long goodsId) {
        QueryWrapper<OrderGoods> query = new QueryWrapper<>();
        query.eq("goods_id", goodsId);
        return baseMapper.selectCount(query) > 0;
    }

    @Override
    public void deleteByOrderId(Long orderId) {
        QueryWrapper<OrderGoods> query = new QueryWrapper<>();
        query.eq("order_id", orderId);
        baseMapper.delete(query);
    }
}
